#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
    int longestOnes(vector<int> &nums, int k)
    {
        int num = 0;
        int maxSize = 0;
        int right = 0, left = 0;
        int len = nums.size();
        for (; right < len; ++right)
        {
            if (!nums[right])
            {
                if (num < k)
                    ++num;
                else
                {
                    maxSize = max(maxSize, right - left);
                    while (left <= right && nums[left++])
                        ;
                }
            }
            // printf("left:%d, right:%d\n",left, right);
        }
        return max(maxSize, right - left);
    }
};